package com.esincrunch.tool;

import java.util.ArrayList;

import com.esincrunch.sms.ContactItem;
import com.esincrunch.sms.SMSItem;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;

public class BlockReader {
	private ArrayList<BlockItem> mBlockList = new ArrayList<BlockItem>();
	
	public BlockItem get(int idx) {
		return mBlockList.get(idx);
	}
	
	int count() {
		return mBlockList.size();
	}
	
	public int read(Activity activity) {
		Cursor cur = activity.managedQuery(ListProvider.CONTENT_URI, null, "typeno=0", null, "sendtime desc");
		if( cur != null && 
			cur.moveToFirst()) {
			BlockItem.initIdx(cur);
			do {
				BlockItem item = new BlockItem(cur);
				mBlockList.add(item);
			} while(cur.moveToNext());
		}
		return count();
	}
	public void delete(String iid,ContentResolver cr ) {
		Uri mUri=Uri.parse(ListProvider.CONTENT_URI + "/" + iid);
		cr.delete(ListProvider.CONTENT_URI, "_id="+iid, null);
	}
	public void deletebyno(String no,ContentResolver cr ) {
		//Uri mUri=Uri.parse(ListProvider.CONTENT_URI + "/" + iid);
		cr.delete(ListProvider.CONTENT_URI, "serviceno="+no, null);
	}
	public int readblk(Activity activity) {
		//String[] projection = new String[]{"_id","serviceno","crdat","typeno"};
		Cursor cur = activity.managedQuery(ListProvider.CONTENT_URI, null, "typeno=0) group by serviceno --(", null, "crdat asc");
		if( cur != null && 
			cur.moveToFirst()) {
			BlockItem.initIdx(cur);
			do {
				BlockItem item = new BlockItem(cur);
				mBlockList.add(item);
			} while(cur.moveToNext());
		}
		return count();
	}
	
	public int readwht(Activity activity) {
		//String[] projection = new String[]{"_id","serviceno","crdat","typeno"};
		Cursor cur = activity.managedQuery(ListProvider.CONTENT_URI, null, "typeno=1) group by serviceno --(", null, "crdat asc");
		if( cur != null && 
			cur.moveToFirst()) {
			BlockItem.initIdx(cur);
			do {
				BlockItem item = new BlockItem(cur);
				mBlockList.add(item);
			} while(cur.moveToNext());
		}
		return count();
	}
	
	public Boolean isInBlockList(String Address,Context context) {
		Cursor cur =context.getContentResolver().query(ListProvider.CONTENT_URI, null, "serviceno='"+Address+"'", null, "sendtime asc");
		if( cur != null && cur.moveToFirst()) {
			return true;
		}
		else{
			return false;
		}
		
	}
	
	public ContactItem getContact(Activity activity, final BlockItem sms) {
		if(sms.mServiceno.length() == 0) return null;
		Cursor cur = activity.managedQuery(ContactsContract.Contacts.CONTENT_URI, 
				new String[] {PhoneLookup.DISPLAY_NAME}, 
				" _id=?", 
				new String[] {sms.mServiceno}, null);
		if(cur != null &&
			cur.moveToFirst()) {
			int idx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
			ContactItem item = new ContactItem();
			item.mName = cur.getString(idx);
			return item;
		}
		return null;
	}

}
